package io.esirong.net;


import org.json.JSONException;

import java.net.HttpURLConnection;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;


/**
 * (响应处理）RespHandler
 * <br>处理HTTPStatusCode 调用{@link #handStatusCode}
 * <br>处理Exception 调用{@link #handResponseError}
 * <br>处理RespCode 调用{@link #handRespCode}
 * <br>处理Response的公用功能 调用{@link #handResp}
 *
 * @author 黄嵘才
 * @date 2017/7/8
 */

public class RespHandler implements Response.Net, Response.Base {
    private static class SingletonHolder {
        @NonNull
        private static RespHandler INSTANCE = new RespHandler();
    }

    /**
     * 业务操作成功吗？
     *
     * @param response 业务响应码
     * @return true 业务操作成功 false 操作失败
     */
    public static boolean isOk(BaseResponse response) {
        return RespCode.Code.SUCCESS.equals(response.code);
    }

    @NonNull
    public static RespHandler instance() {
        return RespHandler.SingletonHolder.INSTANCE;
    }

    /**
     * 处理HTTP 状态码
     *
     * @param httpStatusCode HTTP 状态码
     * @return true 表示处理了这个处理状态码，否则就是没有能够处理
     */
    @Override
    public boolean handStatusCode(int httpStatusCode) {

        switch (httpStatusCode) {
            case HttpURLConnection.HTTP_UNAUTHORIZED:
                //提示用户登录失效。尝试重登录
                break;
            case HttpURLConnection.HTTP_FORBIDDEN:
                showServerForbidden();
                break;
            case HttpURLConnection.HTTP_NOT_FOUND:
                showNotFoundOnServer();
                break;
            case HttpURLConnection.HTTP_BAD_GATEWAY:
                showServerInMaintenance();
                break;
            case HttpURLConnection.HTTP_UNAVAILABLE:
                showServerInMaintenance();
                break;
            case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
                showTimeOut();
                break;
            case HttpURLConnection.HTTP_BAD_REQUEST:
                break;
            default:
                break;
        }

        return false;
    }

    /**
     * 处理服务响应业务(Response) 状态码
     *
     * @param respCode Response 状态码
     * @return true 表示处理了这个处理状态码，否则就是没有能够处理
     */
    @Override
    public boolean handRespCode(String respCode) {
        return false;
    }

    /**
     * 处理服务响应业务(Response)
     * <p>处理业务中公共部分。如错误提示，退出功能等
     *
     * @param response Response
     * @return BaseResponse
     */
    @Override
    public <T> BaseResponse<T> handResp(BaseResponse<T> response) {
        if (response == null) {
            //未知错误
            return response;
        }

        //处理错码,提示错误
        if (!handRespCode(response.code)) {
            String msg = response.message;
        }

        return response;
    }

    /**
     * 处理响应异常E
     *
     * @param error 可拋出的异常
     */
    @Override
    public void handResponseError(Throwable error) {
        if (error instanceof java.net.SocketTimeoutException) {
            showTimeOut();
        } else if (error instanceof javax.net.ssl.SSLException) {
            showServerForbidden();
        } else if (error instanceof java.net.ConnectException) {
            showServerForbidden();
        } else if (error instanceof java.net.UnknownServiceException) {
            showServerForbidden();
        } else if (error instanceof java.net.UnknownHostException) {
            showServerForbidden();
        } else if (error instanceof JSONException) {
            showParseResponseError();
        } else {
            //未捕获异常
            showUnknownError();
        }
    }

    /**
     * 处理后台数据解析错误
     */
    private void showParseResponseError() {

    }

    /**
     * 请求超时
     */
    private void showTimeOut() {

    }

    private void showUnknownError() {

    }

    private void showServerForbidden() {

    }

    private void showNotFoundOnServer() {

    }

    private void showServerInMaintenance() {

    }

    /**
     * 显示浮窗提示信息（Toast)
     *
     * @param msg 信息
     */
    private void showToast(String msg) {

    }

    /**
     * 显示浮窗提示信息（Toast)
     *
     * @param msg 信息
     */
    private void showToast(@StringRes int msg) {

    }

    private void runOnMain() {

    }
}
